package com.ocom.human.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.common.entity.human.EntityRoommateInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.common.vo.human.GetRoommateListVo;
import com.ocom.common.vo.human.GetStudentInfoVo;
import com.ocom.common.vo.human.StudentInfoRVo;
import com.ocom.human.dao.IDaoRoommateInfo;
import com.ocom.human.service.RoommateInfoService;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoommateInfoServiceImpl extends ServiceImpl<IDaoRoommateInfo, EntityRoommateInfo> implements RoommateInfoService {

    @Autowired
    private IDaoRoommateInfo roommateInfo;
    /**
     * 获取同住人列表（getRoommateList）
     *
     * @return*/
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<EntityRoommateInfo> GetRoommateList(Long comId, GetRoommateListVo getRoommateListVo) {
        QueryWrapper<EntityRoommateInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        if (StringUtils.isNotEmpty(getRoommateListVo.getName())){
            query.eq("name",getRoommateListVo.getName());
        }
        if (StringUtils.isNotNull(getRoommateListVo.getPId()) && getRoommateListVo.getPId() > 0){
            query.eq("p_id",getRoommateListVo.getPId());
        }
        query.last("order by com_id,p_id,locate(hs_result,'未核酸,阴性,阳性')  desc,locate(code_color,'绿,黄,红') desc ,hs_time");
        PageHelper.startPage(getRoommateListVo.getPage(),getRoommateListVo.getSize());
        return roommateInfo.selectList(query);
    }

    /**
     * 新增同住人（addRoommate）
     * */
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void AddRoommate(Long comId, EntityRoommateInfo entityRoommateInfo) {
        EntityRoommateInfo one = null;
        if (StringUtils.isNotEmpty(entityRoommateInfo.getOpenid())) {
            QueryWrapper<EntityRoommateInfo> queryWrapper = Wrappers.query();
            queryWrapper.eq("com_id", comId);
            queryWrapper.eq("p_id", entityRoommateInfo.getPId());
            queryWrapper.eq("openid", entityRoommateInfo.getOpenid());
            queryWrapper.last("limit 1");
            one = this.getOne(queryWrapper);
        }
        if (null == one){
            one = entityRoommateInfo;
        } else {
            Long id = one.getId();
            one = entityRoommateInfo;
            one.setId(id);
        }
            this.saveOrUpdate(one);
    }

    /**
     * 删除同住人（removeRoommate）
     * */
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void RemoveRoommate(Long comId, Long pId, Long id) {
        QueryWrapper<EntityRoommateInfo> query = Wrappers.query();
        query.eq("com_id",comId);
        query.eq("p_id",pId);
        if (StringUtils.isNotNull(id)){
            query.eq("id",id);
        }
        roommateInfo.delete(query);
    }
    /**
     * 导入同住人（ importBorders）
     * */
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.MASTER)
    @Override
    public void ImportBorders(Long comId, List<EntityRoommateInfo> entityRoommateInfo) {
        this.saveBatch(entityRoommateInfo);
    }

    /**
     * 获取学生列表  personnel/getStudentInfo
     * */
    @DataSourceAspect(type = DataSourceEnum.PART_DB,companyId = "comId",isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public List<StudentInfoRVo> PersonnelGetStudentInfo(Long comId, GetStudentInfoVo getStudentInfoVo) {
        getStudentInfoVo.setStatus(0);
        List<Integer> deptIdsList = new ArrayList<>();
        if (getStudentInfoVo.getDeptIds() != null && !getStudentInfoVo.getDeptIds().isEmpty()){
            String[] deptIdsArr = getStudentInfoVo.getDeptIds().split(",");
            for (String deptId: deptIdsArr) {
                if (deptId.isEmpty()){
                    continue;
                }
                deptIdsList.add(Integer.valueOf(deptId));
            }
        }
//        String key = MD5.MD5Encode(getStudentInfoVo.getComId().toString(), "utf8");
//        String encryIdentity = null;
//        if (StringUtils.isNotEmpty(getStudentInfoVo.getPIdentity())){
//            encryIdentity = AES.encrypt(getStudentInfoVo.getPIdentity(),key);
//        }
//        String encryPhone = null;
//        if (StringUtils.isNotEmpty(getStudentInfoVo.getPPhone())){
//            encryPhone = AES.encrypt(getStudentInfoVo.getPPhone(),key);
//        }
        PageHelper.startPage(getStudentInfoVo.getPage(),getStudentInfoVo.getSize());
//        return roommateInfo.PersonnelGetStudentInfo(getStudentInfoVo.getComId(),getStudentInfoVo.getPId(),deptIdsList,encryIdentity,
//                encryPhone,getStudentInfoVo.getPCode(),getStudentInfoVo.getPName(),
//                getStudentInfoVo.getStatus(),getStudentInfoVo.getRoomPerson(),getStudentInfoVo.getPType(),
//                getStudentInfoVo.getRegionOrigId(),getStudentInfoVo.getRegionId(),getStudentInfoVo.getRegionOnline() );
        return roommateInfo.PersonnelGetStudentInfo1(getStudentInfoVo.getComId(),getStudentInfoVo.getPId(),deptIdsList,getStudentInfoVo.getPIdentity(),
                getStudentInfoVo.getPPhone(),getStudentInfoVo.getPCode(),getStudentInfoVo.getPName(),
                getStudentInfoVo.getStatus(),getStudentInfoVo.getRoomPerson(),getStudentInfoVo.getPType(),
                getStudentInfoVo.getRegionOrigId(),getStudentInfoVo.getRegionId(),getStudentInfoVo.getRegionOnline() );
    }


}
